import Vue from 'vue'
import VueRouter from 'vue-router'

//import Login from '@/components/Login'
//var Login=()=>import('@/components/Login')

import Welcome from '@/components/Welcome'
// import Users from '@/components/user/Users.vue'

//  角色
// import Roles from '@/components/power/Roles.vue'
// import Rights from '@/components/power/Rights.vue'

/* 商品 */
// import Goods from '@/components/goods/goods.vue'
// import Categories from '@/components/goods/categories.vue'
// import Params from '@/components/goods/params.vue'

/* 订单 */
// import Orders from '@/components/orders/orders.vue'

/* 报表 */
// import Reports from '@/components/reports/reports.vue'

// 添加商品
// import Add from '@/components/goods/Add.vue'

//  非父子
// import family from '@/components/family/family.vue'
Vue.use(VueRouter)



const routes = [
  {
    path: '/', redirect: '/login'
  },
  {
    path: '/login', component: ()=>import(/* webpackChunkName: "login_home_welcome" */'@/components/Login')
  },
  {
    path: '/welcome',
    redirect: '/users',
    component: Welcome,
    children: [
      {
        path: '/users', component: ()=>import(/* webpackChunkName: "Users_Rights_Roles" */'@/components/user/Users.vue')
      },
      {
        path: '/roles', component: ()=>import(/* webpackChunkName: "Users_Rights_Roles" */'@/components/power/Roles.vue')
      },
      {
        path: '/rights', component: ()=>import(/* webpackChunkName: "Users_Rights_Roles" */'@/components/power/Rights.vue')
      },
      {
        path: '/goods', component: ()=>import('@/components/goods/goods.vue')
      },
      {
        path: '/categories', component: ()=>import('@/components/goods/categories.vue')
      },
      {
        path: '/params', component: ()=>()=>import('@/components/goods/params.vue')
      },
      {
        path: '/orders', component: ()=>import('@/components/orders/orders.vue')
      },
      {
        path: '/reports', component: ()=>import('@/components/reports/reports.vue')
      },

      /* 商品添加 */
      {
        path: '/params/add', component: ()=>import('@/components/goods/Add.vue')
      },

      {
        path: '/family', component: ()=>import('@/components/family/family.vue')
      },
      
    ]
  }
]

const router = new VueRouter({
  routes: routes,
  mode: 'hash'
})

router.beforeEach((to, from, next) => {
  // console.log(to,11);
  // console.log(from,22);
  /* 1、获取token
  2、判断to.path!=='login'====判断token
    a、存在token就直接放行
    b、不存在toekn就回到login
  */
  var token = window.sessionStorage.getItem('token')
  if (to.path !== '/login') {
    if (token) {
      next()//  放行可以进入当前路由
    } else {
      next({ path: '/login' })//  //  放行进入login路由
    }
  }
  next()
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
